package com.skp.shaphan.ui.connectioncontrols;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Properties;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.skp.shaphan.actions.SQLServerConnectAction;
import com.skp.shaphan.ui.ActionBasedPanel;
import com.skp.shaphan.ui.ConnectRunnable;

public class SQLServerConnectionOptions extends JPanel implements ActionBasedPanel, ConnectionOptions {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2289701325448216193L;
	private JTextField serverName;
	private JTextField portNumber;
	private JTextField userName;
	private JPasswordField password;
	private SQLServerConnectAction action;
	
	public SQLServerConnectionOptions() {
		this.setLayout(new GridBagLayout());
		JLabel label;
		int row = 0;
		
		label = new JLabel("Server Name: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		serverName = new JTextField();
		this.add(serverName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Port Number: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		portNumber = new JTextField("1433");
		this.add(portNumber, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("User Name");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		userName = new JTextField();
		this.add(userName, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));

		label = new JLabel("Password");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		password = new JPasswordField();
		this.add(password, new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
	}

	@Override
	public ConnectRunnable getAction() {
		if(action == null) {
			action = new SQLServerConnectAction(this);
		}
		return action;
	}
	
	public String getServerName() {
		return serverName.getText();
	}
	public String getPort() {
		return portNumber.getText();
	}
	public String getUser() {
		return userName.getText();
	}
	public String getPassword() {
		return new String(password.getPassword());
	}

	@Override
	public void setConnectionOptions(Properties properties) {
		password.setText(properties.getProperty("password"));
		portNumber.setText(properties.getProperty("portNumber"));
		serverName.setText(properties.getProperty("serverName"));
		userName.setText(properties.getProperty("userName"));
	}

	@Override
	public Properties getConnectionOptions() {
		Properties props = new Properties();
		props.setProperty("password", new String(password.getPassword()));
		props.setProperty("portNumber", portNumber.getText());
		props.setProperty("serverName", serverName.getText());
		props.setProperty("userName", userName.getText());
		return props;
	}

	@Override
	public String getUserFriendlyName() {
		return "SQL Server";
	}

}
